package com.ty.tour.common;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author xxx
 * @date 2018-10-29
 * session自动过期，所有页面验证登录
 */

@SpringBootConfiguration
public class WebConfig extends WebMvcConfigurationSupport {

    //将拦截器手动注解为bean 为力自动注入拦截器里的RedisUtils
    @Bean
    public DiyConfig diyConfig() {
        return new DiyConfig();
    }

//    // Shiro保存的用户对象
//    public static final String USER_SESSION = "userSession";

    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(diyConfig()).excludePathPatterns("/**");
//                .addPathPatterns("/**")
//                .excludePathPatterns("/error")
//                .excludePathPatterns("/zkapis/**")
//                .excludePathPatterns("/user/login**")
//                .excludePathPatterns("/user/w/unbundling")
//                .excludePathPatterns("/sip/UpdateBackCode")
//                .excludePathPatterns("/sip/UpdateFrontCode")
//                .excludePathPatterns("/user-info/login")
//                .excludePathPatterns("/user-info/**")
//                .excludePathPatterns("/wechat/**")
//                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**","/swager/**")
//                .excludePathPatterns("/static/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将templates目录下的CSS、JS文件映射为静态资源，防止Spring把这些资源识别成thymeleaf模版
        registry.addResourceHandler("/swagger/**.js").addResourceLocations("classpath:/swagger/");
        registry.addResourceHandler("/swagger/**.css").addResourceLocations("classpath:/swagger/");
        //其他静态资源
        registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/swagger/");
        //swagger增加url映射
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/resources/").addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/");

        super.addResourceHandlers(registry);
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("*")
                .maxAge(3600);
    }

}
